//package com.xiangxiangjd.config.shiro;
//
//import com.xiangxiangjd.bean.AdminUser;
//import com.xiangxiangjd.bean.Permission;
//import com.xiangxiangjd.service.AdminUserService;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import java.util.HashSet;
//import java.util.List;
//import java.util.Set;
//
///**
// * @author 肖守凯
// * @packge com.xsy.config.shiro
// * @data 2019-04-08 12:04
// * @project GoodLuck
// */
//@Slf4j
//public class ShiroRealm extends AuthorizingRealm {
//    @Autowired
//    AdminUserService adminUserService;
//
//    /**
//     * 授权
//     * @param principalCollection
//     * @return
//     */
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        //此方法中的值 为doGetAuthenticationInfo 存入的user
//        AdminUser adminUser = (AdminUser) principalCollection.getPrimaryPrincipal();
//
//            if(adminUser.getRole()==null){
//                return  simpleAuthorizationInfo;
//            }
////        从shiro中取出登陆成功后的用户信息   并把角色名 给shiro控制
////        权限
//        Set<String> roleSet = new HashSet<String>();
//            log.debug("当前登陆的用户角色为：{}", adminUser.getRole().getRoleName());
//        roleSet.add("role:" + adminUser.getRole().getRoleName());
//        simpleAuthorizationInfo.addRoles(roleSet);
//        List<Permission> menuResourse = adminUser.getRole().getPermissions();
//
//
//        for (int i = 0; i < menuResourse.size(); i++) {
//            if(menuResourse.get(i).getMenuType()==2){
//                simpleAuthorizationInfo.addStringPermission("button:" + menuResourse.get(i).getName());
//            }
//            simpleAuthorizationInfo.addStringPermission("menu:" + menuResourse.get(i).getName());
//        }
//
//
//        return simpleAuthorizationInfo;
//    }
//
//    /**
//     * 鉴权
//     * @param authenticationToken
//     * @return
//     * @throws AuthenticationException
//     */
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)  {
//        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
//        String requestUserName = token.getUsername();
//        String requestPassWord = new String(token.getPassword());
//        //持久层方法
//        AdminUser adminUser = adminUserService.UserLogin(requestUserName,requestPassWord);
//
//
//
//
//        if(adminUser!=null) {
//            System.out.println("鉴定了权限" + adminUser.toString());
//        }
//
//        log.debug("当前登陆的用户信息为：{}", adminUser.toString());
//
//
//        // 踢出已登录的用户
//   /*     DefaultWebSecurityManager securityManager =  (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();
//        DefaultWebSessionManager sessionManager = (DefaultWebSessionManager) securityManager.getSessionManager();
//        MemorySessionDAO sessionDao = (MemorySessionDAO) sessionManager.getSessionDAO();
//        Collection<Session> sessions = sessionDao.getActiveSessions();
//        for (Session session : sessions) {
//            if (SecurityUtils.getSubject().getSession().getId().equals(session.getId())){//如果登陆的session有本次登陆的人
//
//            }else{
//                sessionManager.getSessionDAO().delete(session);
//            }
//
//        }
//        for(Session session:sessions){
//            System.out.println("登录ip:"+session.getHost());
//            System.out.println("登录用户"+session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY));
//            System.out.println("最后操作日期:"+session.getLastAccessTime());
//        }
//*/
//
//        //这里只需要传入 数据库中查出的对象和 密码即可
//        SimpleAuthenticationInfo authorizationInfo = new SimpleAuthenticationInfo(
//                adminUser,  //这里传对象
//                adminUser.getPassWord(),
//                getName()
//        );
//        return authorizationInfo;
//    }
//}
